/** 
 * All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
 * 
 *CbbPmRiskController
 * 项目风险
 * 
 * 修改纪录
 * 2023-06-26 版本：1.0 weixinmin 创建。
 * @version 版本：1.0
 * @author 作者：weixinmin
 * 创建日期：2023-06-26
 */

package com.topscomm.mis.cbb.pm.controller;

import com.topscomm.mis.cbb.pm.api.IDeliverableService;
import com.topscomm.mis.cbb.pm.api.IRiskService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmDeliverableEntity;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmRiskEntity;
import com.topscomm.mis.cbb.pm.controller.auto.CbbPmRiskControllerAuto;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.constant.TapHttpErrorCode;
import com.topscomm.tap.constant.TapHttpStatus;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: weixinmin
 * @date: 2023-06-26
 * @description:项目风险Controller
 * @modify:
 */
@RestController
@RequestMapping("base/cbbPmRisk")
public class CbbPmRiskController extends CbbPmRiskControllerAuto {

	@Autowired
	private IRiskService riskService;

	/**
	 * @author: weixinmin
	 * @date: 2023-06-26
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}

		// 项目号
		String value = paraMap.get(CbbPmRiskEntity.FieldProjectuucode).toString();
		if (!StringUtil.isEmpty(value)) {
			sql.appendSingle(" and projectuucode = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * description 列表页
	 * @author wxm
	 * @date 2023-06-25 17:28:20
	 * @param paraMap
	 * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify
	 */
	@GetMapping(value = "/list")
	public ResponseResult<Object> list(
		@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			BaseUtil.decodeSecureMap(paraMap);
			String sqlWhere = this.spellListSql(paraMap);
			Page page = riskService.list(paraMap, sqlWhere);
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return ResponseResult
				.error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR, ex.getMessage());
		}
	}

	/**
	 * description 加载详情页
	 * @author wxm
	 * @date 2023-06-25 18:45:39
	 * @param paraMap
	 * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify
	 */
	@GetMapping(value = "/load")
	public ResponseResult<Object> load(@RequestParam(required = false) Map<String, Object> paraMap) {
		BaseUtil.decodeSecureMap(paraMap);
		Map<String, Object> dataMap = riskService.load(paraMap);
		return ResponseResult.ok(dataMap);
	}

	/**
	 * description 插入数据
	 * @author wxm
	 * @date 2023-06-25 18:14:30
	 * @param paraMap
	 * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify
	 */
	@PostMapping(value = "/insert")
	public ResponseResult<Object> insert(
		@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> resMap = riskService.insert(paraMap);
			return ResponseResult.ok(resMap);
		} catch (Exception e) {
			return ResponseResult
				.error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR, e.getMessage());
		}

	}

	/**
	 * description 更新
	 * @author wxm
	 * @date 2023-06-25 18:17:00
	 * @param paraMap
	 * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify
	 */
	@PutMapping(value = "/update")
	public ResponseResult<Object> update(
		@RequestBody(required = false) Map<String, Object> paraMap) {
		try {
			riskService.update(paraMap);
			return ResponseResult.ok("修改成功");
		}catch (Exception e){
			return ResponseResult
				.error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR, e.getMessage());
		}
	}

	/**
	 * description 删除
	 * @author wxm
	 * @date 2023-06-25 18:20:48
	 * @param paraMap
	 * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
	 * @modify
	 */
	@DeleteMapping(value = "/delete")
	public ResponseResult<Object> delete(@RequestBody(required = false) Map<String, Object> paraMap) {
		BaseUtil.decodeSecureMap(paraMap);
		riskService.delete(paraMap);
		return ResponseResult.ok("删除成功");
	}

}

